package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.domain.entity.SysDictData;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.utils.DictUtils;
import com.lvy.hczn.front.system.domain.business.PurchaseOrder;
import com.lvy.hczn.front.business.mapper.PurchaseOrderMapper;
import com.lvy.hczn.front.business.service.PurchaseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements PurchaseOrderService {

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 检查当前是否有入库单正在入库
     *
     * @Description
     * @Param
     * @Author yw
     * @Date 2024/6/13 10:21
     * @Return
     **/
    @Override
    public ResultUtil checkPurchaseEntry(Param param) {

        JSONArray array = (JSONArray) redisCache.get(CacheConstants.DICT_TYPE_WH_COM);
        //存在在数据字典中，则发送到前端
        boolean exist = false;
        SysDictData dataInfo = new SysDictData();
        String deliveryCode = "";
        /*获取上一个下架货箱的仓库index**/
        if (array != null && !array.isEmpty()) {
            for (Object o : array) {
                SysDictData data = (SysDictData) o;
                String keyInfo = CacheConstants.REDIS_KEY_SOCKET_CLIENT + ":" + data.getDictValue() + ":deliverOrderCode";
                String resultInfo = (String) redisCache.get(keyInfo);
                if (!StrUtil.isEmpty(resultInfo)) {
                    //同一个仓库，只能有同一个入库单，但是两个仓库可以用不同入库单，即：货架和地平仓可以同时入库
                    //根据访问的页面和电脑设置的值判断
                    //进入的是大件的页面且电脑值是负数
                    if ((param.getBigProduct().equals(com.lvy.hczn.front.common.constant.Constants.GOODS_COLLECT_YES) && Integer.parseInt(data.getDictValue()) < 0) || (param.getBigProduct().equals(com.lvy.hczn.front.common.constant.Constants.GOODS_COLLECT_NO) && Integer.parseInt(data.getDictValue()) > 0)) {
                        exist = true;
                        dataInfo = data;
                        deliveryCode = resultInfo;
                        break;
                    }

                }
            }
        }
        if (!exist) {
            //无采购入库单据，可以进入页面
            return ResultUtil.ok();
        } else {
            /*有采购入库单据，且入库电脑一致可进入*/
            if (dataInfo.getDictValue().equals(param.getUpOrDownPolicy().getClientId())) {
                return ResultUtil.ok();
            } else {
                return ResultUtil.failed(deliveryCode, "");
            }
        }
    }
}
